﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ToodledoAPI
{
    public class UnixTime:IEquatable<UnixTime>
    {
        private UnixTime(long value)
        {
            Value = value;
        }

        public static UnixTime Parse(string TimeStamp)
        {
            return new UnixTime(long.Parse(TimeStamp));
        }

        static readonly DateTime Epoch = new DateTime(1970, 1, 1);

        public long Value { get; set; }

        public static implicit operator DateTime(UnixTime UT)
        {
            return Epoch.AddSeconds(UT.Value);
        }
        public static implicit operator UnixTime(DateTime DT)
        {
            return new UnixTime((long)Math.Round(DT.Subtract(Epoch).TotalSeconds));
        }

        public static implicit operator long(UnixTime UT)
        {
            return UT.Value;
        }
        public static implicit operator UnixTime(long L)
        {
            return new UnixTime(L);
        }

        public override string ToString()
        {
            return Value.ToString();
        }


        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return false;
            }
            return Equals((UnixTime)obj);
        }

        public override int GetHashCode()
        {
            return Value.GetHashCode();
        }

        public bool Equals(UnixTime other)
        {
            return Value == other.Value;
        }
        
        public static bool operator ==(UnixTime UT1, UnixTime UT2)
        {
            if((object)UT1 == null)
                return ((object)UT2==null);
            else
                return UT1.Equals(UT2);
        }
        public static bool operator !=(UnixTime UT1, UnixTime UT2)
        {
            if ((object)UT1 == null)
                return ((object)UT2 != null);
            else
                return !UT1.Equals(UT2);
        }
    }
}
